package com.team6.cet.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.UIManager;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team6.cet.entity.IStatusMessage;
import com.team6.cet.entity.ResponseResult;
import com.team6.cet.entity.Userenrollment;
import com.team6.cet.mapper.UserenrollmentMapper;
import com.team6.cet.service.UserenrollmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.ehcache.search.aggregator.Count;

/**
 * @author 敖海洋 oceanao@qq.com
 * @date 创建时间：2018年4月19日 上午11:58:16 
 * @version 1.0 
 * @telephone 18681660783 
 */
@CrossOrigin(origins = "*")
@RestController
@EnableAutoConfiguration
@Api(tags = "用户学籍api")
@Validated
/**
 * 用户信息控制类*/
public class UserenrollmentController {

	@Autowired
	private UserenrollmentService uService;
	@Autowired
	private UserenrollmentMapper uMapper;
	@ApiOperation(value = "查询所有学籍信息")  
	@GetMapping("/userenrollment")
	public Object getUserenrollment()
	{
		Map map=new HashMap();
		List<Userenrollment> Userenrollment=uService.findUserenrollment();
		map.put("data", Userenrollment);
		map.put("code", 0);
		map.put("count",uMapper.findNum());
		return  map;
	}
	
	@ApiOperation(value = "查询所有学籍信息")  
	@GetMapping("/userenrollmentidnum")
	public Object UserenrollmentidNum(@RequestParam("idnum")String idNum)
	{
		Map map=new HashMap();
		Userenrollment Userenrollment= uService.findByidNum(idNum);
		if(Userenrollment==null)
			map.put("count",0);
		else
			map.put("count",1);
		List<Userenrollment> User=new LinkedList<Userenrollment>();
		User.add(Userenrollment);		
		map.put("data", User);
		map.put("code", 0);
		return  map;
	}
	
	@ApiOperation(value = "查询所有学籍信息")  
	@GetMapping("/userinfoidnum")
	public ResponseResult userinfoidnum(@RequestParam("idnum")String idNum)
	{
		 
		Userenrollment Userenrollment= uService.findByidNum(idNum);
		 ResponseResult responseResult = new ResponseResult();
			responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
			responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
			responseResult.setMessage("添加成功");
			responseResult.setObj(Userenrollment);
		return responseResult;
	}
	/**
	 * 添加学籍信息*/
	@ApiOperation(value = "添加学籍信息")  
	@PostMapping("/adduserenrollment")
	public ResponseResult addUserenrollment(@ApiParam(required = true, value = "添加学籍信息")@RequestBody @Valid Userenrollment user)
	{
		 uService.insertUserenrollment(user);
		 ResponseResult responseResult = new ResponseResult();
			responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
			responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
			responseResult.setMessage("添加成功");
		return responseResult;
	}
	@ApiOperation(value = "根据id查询学籍信息")  
	@GetMapping("/userenrollmentbyid")
	public Object Userenrollmentbyid(@RequestParam("id")Integer id)
	{
		Map map=new HashMap();
		 Userenrollment user=uService.findById(id);
		 map.put("msg", user);
	    return  map;
	}
	
	@ApiOperation(value = "删除学籍信息")  
	@PutMapping("/deluser")
	public Object delUserenrollment(@RequestParam("id")Integer id)
	{
		Map map=new HashMap();
		 uService.delUserenrollment(id);
		 map.put("msg", "ok");
	    return  map;
	}
	
	@ApiOperation(value = "修改用户报考状态成功")  
	@GetMapping("/edituserbyid")
	public ResponseResult edituserbyid(@RequestParam("id")Integer id)
	{
		 
		Userenrollment user= uService.findById(id);
		if (user.getUserStatus().equals("可以报名")) {
			user.setUserStatus("不可以报名");
		}
		else{
			user.setUserStatus("可以报名");
		}
		uService.editUserenrollment(user);
		 ResponseResult responseResult = new ResponseResult();
			responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
			responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
			responseResult.setMessage("成功");
		return responseResult;
	}
}
